package com.example.controller;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.entity.Rule;
import com.example.entity.Student;
import com.example.mapper.StudentMapper;
import com.example.mapper.RuleMapper;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/apply3")
public class ApplicationController3 {
    @Resource
    StudentMapper studentMapper;
    @Resource
    RuleMapper ruleMapper;

    @PostMapping("/Grade3")
    public Map<String, String> save3(@RequestBody String info){ //接受JSON类数据

        JSONObject studentjsoninfo= JSONObject.parseObject(info);  // 将前端学生信息解析为一个JSONObject对象并返回

        String syear = studentjsoninfo.getString("年份");

        String  grade3 = "研三";

        QueryWrapper<Rule> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ruler_grade",grade3).eq("ruler_year",syear);


        Rule ruleyear = ruleMapper.selectOne(queryWrapper);  //取得对应年级的rule
        String rules = ruleyear.getRule_info();
        JSONObject rulejson = JSONObject.parseObject(rules);    //把写好的rule转换为JSON便于计算


        //研三无学业成绩，学业成绩部分省略，其他部分与研二基本相同

        //思政表现（日常分）
        JSONObject regularrule = (JSONObject) (rulejson.get("思政表现")); //规则的思政表现
        JSONObject regularscore = (JSONObject) (studentjsoninfo.get("思政表现")); //学生的思政表现

        //扣分项
        int criticizescore = Integer.parseInt(regularrule.get("通报每次所扣分数").toString());;  //从规则中找出这部分设定分数
        int criticizetimes = Integer.parseInt(regularscore.get("通报次数").toString());
        int criticizetotal = criticizescore * criticizetimes;


        //思政总分 = 基础分（30）- 通报总扣分
        int basicscore = 30;
        int regulartotal = basicscore - criticizetotal;
        if(regulartotal < 0){ regulartotal = 0; };
        if(regulartotal > 30) { regulartotal = 30; }

        double studenttotal  =  regulartotal;


        //科研能力（30） =  学术论文分数 + 学科竞赛分数  （简化版）
        JSONObject sciencerule = (JSONObject) (rulejson.get("科研能力"));  // 规则的科研能力
        JSONObject sciencescore = (JSONObject) (studentjsoninfo.get("科研能力")); //学生的科研能力


        //学术论文部分
        JSONObject papervalue = (JSONObject) (sciencerule.get("学术论文")); //规则中的学术论文的分数 ，变量采用value
        double paper1value = Double.parseDouble(papervalue.get("CCF推荐A类国际学术期刊或中科院1区").toString());
        double paper2value = Double.parseDouble(papervalue.get("CCF推荐B类国际学术期刊或中科院2区").toString());
        double paper3value = Double.parseDouble(papervalue.get("CCF推荐C类国际学术期刊或中科院3区").toString());
        double paper4value = Double.parseDouble(papervalue.get("一般学术期刊").toString());

        JSONArray paperscore = sciencescore.getJSONArray("学术论文");  // 学生信息处返回得到的学术论文，Array可包含多个JSONObject

        double paper1 = 0 ;  // 赋初值为0
        double paper2 = 0 ;
        double paper3 = 0 ;
        double paper4 = 0 ;

        for (Object paper : paperscore){
            JSONObject paperScore = (JSONObject) paper; //学生具体返回的学术论文 类型
            switch (paperScore.getString("类型")){
                //根据类型计算分数
                case "CCF推荐A类国际学术期刊或中科院1区" : paper1 = paper1 + 1;
                case "CCF推荐B类国际学术期刊或中科院2区" : paper2 = paper2 + 1;
                case "CCF推荐C类国际学术期刊或中科院3区" : paper3 = paper3 + 1;
                case "一般学术期刊" : paper4 = paper4 + 1;
            }
        }

        //学术论文分数 = 每篇论文分数相加
        double papertotal = paper1* paper1value + paper2*paper2value + paper3*paper3value + paper4*paper4value ;

        //学科竞赛部分（与论文申报类似）
        JSONObject pricevalue = (JSONObject) (sciencerule.get("学科竞赛"));
        double price1value = Double.parseDouble(pricevalue.get("国家级一等奖").toString());
        double price2value = Double.parseDouble(pricevalue.get("国家级二等奖").toString());
        double price3value = Double.parseDouble(pricevalue.get("国家级三等奖").toString());
        double price4value = Double.parseDouble(pricevalue.get("省级一等奖").toString());
        double price5value = Double.parseDouble(pricevalue.get("省级二等奖").toString());
        double price6value = Double.parseDouble(pricevalue.get("省级三等奖").toString());
        double price7value = Double.parseDouble(pricevalue.get("校赛奖项").toString());

        JSONArray pricescore =  sciencescore.getJSONArray("学科竞赛"); //学生信息处返回得到的学科竞赛

        double price1 = 0;
        double price2 = 0;
        double price3 = 0;
        double price4 = 0;
        double price5 = 0;
        double price6 = 0;
        double price7 = 0;

        for (Object price : pricescore){
            JSONObject priceScore = (JSONObject) price;
            switch (priceScore.getString("类型")){

                case "国家级一等奖":  price1 =  price1 + 1;
                case "国家级二等奖":  price2 =  price2 + 1;
                case "国家级三等奖":  price3 =  price3 + 1;
                case "省级一等奖" :   price4 =  price4 + 1;
                case "省级二等奖" :   price5 =  price5 + 1;
                case "省级三等奖" :   price6 =  price6 + 1;
                case "校赛奖项"   :   price7 =  price7 + 1;

            }
        }

        double pricetotal = price1*price1value + price2*price2value + price3*price3value + price4*price4value + price5*price5value + price6*price6value + price7*price7value;

        double sciencetotal = papertotal + pricetotal ;

        if( sciencetotal > 50 ) {sciencetotal = 50;}

        studenttotal  = studenttotal + sciencetotal;


        //社会服务分数 （20） = 基础分（15） +  附加分（5）

        JSONObject socialrule = (JSONObject) (rulejson.get("社会服务"));
        JSONObject socialscore = (JSONObject) (studentjsoninfo.get("社会服务"));

        int basicsocialscore = 15;
        int volunteervalue = Integer.parseInt(socialrule .get("附加分数").toString());
        int volunteertimes = Integer.parseInt(regularscore.get("志愿次数").toString());

        int volunteertotal = volunteervalue *  volunteertimes;
        int socialtotal =  basicsocialscore + volunteertotal;


        //总分经过三次计算，得出！
        studenttotal = studenttotal +  socialtotal;

        String student_id = studentjsoninfo.getString("学生学号");

        Student student = studentMapper.selectOne(new QueryWrapper<Student>().eq("student_id", student_id));

        //查询到学生后，自动把总分赋予该学生
        if (student != null) {
            student.setStudent_total(studenttotal);
            studentMapper.updateById(student);
        }


        Map<String, String>  resultMap = new HashMap<>();
        resultMap.put("申请状态", "success");

        return resultMap;

    }











}


